package cn.stylefeng.guns.modular.api;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.stylefeng.guns.modular.system.model.BusinessCompany;
import cn.stylefeng.guns.modular.system.service.IBusinessCompanyService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 商家信息控制器
 *
 * @author glb
 * @Date 2019-10-23 18:32:21
 */
@Controller
@RequestMapping("/api/v1/businessCompanys")
@Api(value = "家政公司信息",tags={"家政公司操作接口"})
public class BusinessCompanyApiController extends BaseController {


    @Autowired
    private IBusinessCompanyService businessCompanyService;

    /**
     * 获取商家信息列表
     */
    @ApiOperation(value="获取家政公司列表", notes="")
    @RequestMapping(value = "/",method=RequestMethod.GET)
    @ResponseBody
    public Object list(@ModelAttribute BusinessCompany businessCompany) {
    	EntityWrapper<BusinessCompany> wrapper = new EntityWrapper<>();
    	if(businessCompany !=null) {
    		Integer	id=	businessCompany.getId();
    		if(id !=null ) {
    			wrapper.eq("id", id);
        	}
    	      String 	name= businessCompany.getName();	
    		if(StringUtils.isNotBlank(name)) {
    			wrapper.like("name", name);
    		}
    	     
    	     String 	phone= businessCompany.getPhone();	
     		if(StringUtils.isNotBlank(phone)) {
     			wrapper.like("phone", phone);
     		}
     		 String 	place= businessCompany.getPlace();	
      		if(StringUtils.isNotBlank(place)) {
      			wrapper.like("place", place);
      		}
      		 String 	contacts= businessCompany.getContacts();
       		if(StringUtils.isNotBlank(contacts)) {
       			wrapper.like("contacts", contacts);
       		}
    		
       	    String cateId= businessCompany.getCateId();
    		if(StringUtils.isNotBlank(cateId)) {
    			wrapper.like("cate_id", cateId);
    		}
 		    
    		Integer version= businessCompany.getVersion();
     		if(version !=null) {
     			wrapper.eq("version", version);
     		}
       	
     		String ext	=businessCompany.getExt();
     		if(StringUtils.isNotBlank(ext)) {
     			wrapper.like("ext", ext);
     		}
     		Integer isValid= businessCompany.getIsValid();
     		if(isValid !=null) {
     			wrapper.eq("is_valid", isValid);
     		}
    		
     		String remark	=businessCompany.getRemark();
     		if(StringUtils.isNotBlank(remark)) {
     			wrapper.like("remark", remark);
     		}
     		return businessCompanyService.selectList(wrapper);
    	}
           return  null;
    }

    /**
     * 新增商家信息
     */
    @ApiOperation(value="创建家政公司", notes="根据BusinessCompany对象创建家政大类")
    @RequestMapping(value = "/",method=RequestMethod.POST)
    @ResponseBody
    public Object add(@RequestBody BusinessCompany businessCompany) {
    	Date now =  new Date();
    	businessCompany.setCreateTime(now);
    	businessCompany.setUpdateTime(now);
    	//TODO  更新和创建用户
        businessCompanyService.insert(businessCompany);
        return SUCCESS_TIP;
    }

    /**
     * 删除商家信息
     */
    
    @ApiOperation(value="删除家政公司信息", notes="根据url的id来指定删除家政公司对象")
    @ApiImplicitParam(name = "id", value ="家政公司ID", required = true ,dataType ="int",  example="1")
    @RequestMapping(value = "/{id}",method=RequestMethod.DELETE)
    @ResponseBody
    public Object delete(@PathVariable Integer id) {
        businessCompanyService.deleteById(id);
        return SUCCESS_TIP;
    }

    /**
     * 修改商家信息
     */
   
    @ApiOperation(value="更新家政公司详细信息", notes="根据url的id来指定更新对象，并根据传过来的BusinessCompany信息来更新家政公司详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "家政公司ID", required = true,dataType ="int",  example="1")
    })
    @RequestMapping(value = "/{id}",method=RequestMethod.PUT)
    @ResponseBody
    public Object update(@PathVariable("id") Integer id,  @ModelAttribute BusinessCompany businessCompany) {
        businessCompany.setId(id);
        businessCompany.setUpdateTime( new Date());
         //TODO   更新用户
        businessCompanyService.updateById(businessCompany);
        return SUCCESS_TIP;
    }

    /**
     * 商家信息详情
     */
    @ApiOperation(value="获取家政公司详细信息", notes="根据url的id来获取家政公司详细信息")
    @ApiImplicitParam(name = "id", value = "家政公司ID", required = true,dataType ="int",  example="1")
    @RequestMapping(value = "/{id}",method=RequestMethod.GET)
    @ResponseBody
    public Object detail(@PathVariable("id") Integer id) {
        return businessCompanyService.selectById(id);
    }
}
